package  
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import sourceproxy.ResourceProxy;
	import sourceproxy.ResourceProxyEventType;
	/**
	 * ...
	 * @author ...
	 */
	public class MMImage extends MMInteractive 
	{
		private var _bitmap:Bitmap;
		private var _bitmapData:BitmapData;
		private var _source:String;
		
		public var onChange:Function;
		
		
		public function MMImage(bitmapData:BitmapData=null) 
		{
			_bitmapData = bitmapData;
			show();
		}
		
		override public function onInit():void
		{
			super.onInit();
			_bitmap = new Bitmap();
			addChild(_bitmap);
		}
		
		override public function onHide():void
		{
			super.onHide();
		}
		
		override public function onShow():void
		{
			super.onShow();
			update();
		}
		
		override public function updateData():void
		{
			super.updateData();
			
			if (_bitmap.bitmapData != _bitmapData)
			{
				_bitmap.bitmapData = _bitmapData;
				width = _bitmap.width;
				height = _bitmap.height;
				onChange && onChange();
			}
			
		}
		
		public function set source(url:String):void
		{
			if (_source == url) return;
			_source = url;
			
			var bitmapData:BitmapData = ResourceProxy.getBitmapData(url);
			if (!bitmapData)
			{
				var callback:Function=function ():void
					{
						ResourceProxy.instance().removeEventListener(ResourceProxyEventType.COMPLETE, callback);
						_bitmapData = ResourceProxy.getBitmapData(url);
						update();
					}
				ResourceProxy.instance().push(url);
				ResourceProxy.instance().onComplete = callback;
				ResourceProxy.instance().load();
			}
			else
			{
				_bitmapData = bitmapData;
				update();
			}
			
			
		}
		public function get source():String
		{
			return _source;
		}
		
		public function get bitmapData():BitmapData { return _bitmapData; }
		
		public function set bitmapData(value:BitmapData):void 
		{
			_bitmapData = value;
			update();
		}
			
}

}